from typing import List


class Solution:
    def maxCoins(self, nums: List[int]) -> int:
        nums = [1] + nums + [1]
        size = len(nums)

        dp = [[0] * size for _ in range(size)]

        for length in range(2, size):
            for i in range(size - length):
                j = i + length
                for k in range(i + 1, j):
                    dp[i][j] = max(dp[i][j], nums[i] * nums[k] * nums[j] + dp[i][k] + dp[k][j])

        # for row in dp:
        #     print(row)

        return dp[0][-1]


if __name__ == "__main__":
    print(Solution().maxCoins([3, 1, 5, 8]))  # 167
    print(Solution().maxCoins([1, 5]))  # 10
